package com.cenntro.oss.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.curator.shaded.com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONObject;
import com.cenntro.common.base.BaseServiceImpl;
import com.cenntro.common.mapper.SysconfMapper;
import com.cenntro.common.model.dbo.SysconfDO;
import com.cenntro.common.util.result.Result;
import com.cenntro.oss.common.constants.ImportDataConstants;
import com.cenntro.oss.common.enums.OnlineDeviceStatusEnum;
import com.cenntro.oss.common.enums.StaticEnum;
import com.cenntro.oss.mapper.DevAssemblyPlantMapper;
import com.cenntro.oss.mapper.DevBatteryMapper;
import com.cenntro.oss.mapper.DevBatteryModuleMapper;
import com.cenntro.oss.mapper.DevBatteryPackMapper;
import com.cenntro.oss.mapper.DevBomCarMapper;
import com.cenntro.oss.mapper.DevBomPartpnRefMapper;
import com.cenntro.oss.mapper.DevCarMapper;
import com.cenntro.oss.mapper.DevCertificateMapper;
import com.cenntro.oss.mapper.DevDeviceOnlineMapper;
import com.cenntro.oss.mapper.DevDevicePartsMapper;
import com.cenntro.oss.mapper.DevDeviceStaticMapper;
import com.cenntro.oss.mapper.DevPartsMapper;
import com.cenntro.oss.mapper.DevSupplyImeiMapper;
import com.cenntro.oss.mapper.ExcelUploadMapper;
import com.cenntro.oss.model.dbo.DevBatteryDO;
import com.cenntro.oss.model.dbo.DevBatteryModuleDO;
import com.cenntro.oss.model.dbo.DevBatteryPackDO;
import com.cenntro.oss.model.dbo.DevBomCarDO;
import com.cenntro.oss.model.dbo.DevBomPartpnRefDO;
import com.cenntro.oss.model.dbo.DevCarDO;
import com.cenntro.oss.model.dbo.DevCertificateDO;
import com.cenntro.oss.model.dbo.DevDeviceOnlineDO;
import com.cenntro.oss.model.dbo.DevDevicePartsDO;
import com.cenntro.oss.model.dbo.DevDeviceStaticDO;
import com.cenntro.oss.model.dbo.DevSupplyImeiDO;
import com.cenntro.oss.service.ExcelUploadService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

@Service
@Transactional
public class ExcelUploadServiceImpl extends BaseServiceImpl<ExcelUploadMapper> implements ExcelUploadService {

	@Resource
	private DevDeviceStaticMapper devDeviceStaticMapper;

	@Resource
	private DevCarMapper devCarMapper;

	@Resource
	private DevAssemblyPlantMapper devAssemblyPlantMapper;

	@Resource
	private DevCertificateMapper devCertificateMapper;

	@Resource
	private DevBatteryPackMapper devBatteryPackMapper;

	@Resource
	private SysconfMapper sysconfMapper;

	@Resource
	private DevPartsMapper devPartsMapper;

	@Resource
	private DevSupplyImeiMapper devSupplyImeiMapper;

	@Resource
	private DevDevicePartsMapper devDevicePartsMapper;

	@Resource
	private DevBatteryModuleMapper devBatteryModuleMapper;

	@Resource
	private DevBatteryMapper devBatteryMapper;

	@Resource
	private DevBomCarMapper devBomCarMapper;

	@Resource
	private DevBomPartpnRefMapper devBomPartpnRefMapper;

	@Resource
	private DevDeviceOnlineMapper devDeviceOnlineMapper;
	@Resource
	private ValidExcel validExcel;


	@Transactional(rollbackFor = Exception.class)
	@Override
	public Result importDevice(String str) {
		List<String> color = validSysConf("color");
		List<String> unlockWay = validSysConf("unlock_way");
		List<String> plant = validSysConf("assembly_plant");

		Map<String, Integer> mapCarType = validCarId();
		Map<String, String> mapPlant = validPlant();
		// 转换为目标对象list
		List<DevDeviceStaticDO> list = new ArrayList<>();
		// List<DevDeviceStaticDO> listDevDeviceStaticDO = new ArrayList<>();
		// List<DevDeviceOnlineDO> listDevDeviceOnlineDO = new ArrayList<>();
		try {
			list = JSONObject.parseArray(str, DevDeviceStaticDO.class);
		} catch (Exception e) {
			String[] split = e.toString().split(":");
			String msg = split[split.length - 1].replace("\"", "");
			Assert.isTrue(false, ImportDataConstants.BUILDTIME_FORMAT_ERROR + ":" + msg);
		}
		int i = ImportDataConstants.START_ROW + 1;
		validExcel.validDevice(list, color, unlockWay, mapCarType, plant, 1);
		DevSupplyImeiDO dsido = new DevSupplyImeiDO();
		for (DevDeviceStaticDO dsdo : list) {
			i++;
			dsdo.setCarId(mapCarType.get(dsdo.getCarType()));
			DevBomCarDO DevBomCarDO = devBomCarMapper.selectOne(ImmutableMap.of("carId", dsdo.getCarId(), "bomVersion", dsdo.getBomVersion()));
			Assert.isTrue(DevBomCarDO != null, ImportDataConstants.splicing(i, ImportDataConstants.BOM_VERSION_UNMATCH));
			if (StringUtils.isNotBlank(dsdo.getVin())) {
				int count = devDeviceStaticMapper.selectCount(ImmutableMap.of("carNub", dsdo.getVin()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.CARNUB_EXIST));
			}

			dsdo.setBomId(DevBomCarDO.getBomId());
			dsdo.setStatus(StaticEnum.NO_DISTRIBUTION.getState());
			dsdo.setCdt(new Date());
			dsdo.setDapCity(mapPlant.get(dsdo.getDapName()));
			devDeviceStaticMapper.insert(dsdo);
			// listDevDeviceStaticDO.add(dsdo);
			DevDeviceOnlineDO DevDeviceOnlineDO = new DevDeviceOnlineDO();
			DevDeviceOnlineDO.setDevId(dsdo.getDevId());
			DevDeviceOnlineDO.setDeviceStatus(OnlineDeviceStatusEnum.OTHER.getState());
			DevDeviceOnlineDO.setCreateTime(new Date());
			// listDevDeviceOnlineDO.add(DevDeviceOnlineDO);
			devDeviceOnlineMapper.insert(DevDeviceOnlineDO);
			dsido = devSupplyImeiMapper.selectOne(ImmutableMap.of("imei", dsdo.getImei()));
			DevSupplyImeiDO dsi = new DevSupplyImeiDO();
			dsi.setState(1);
			dsi.setImei(dsdo.getImei());
			dsi.setAllotion(1);
			if (dsido == null) {
				dsi.setCreateTime(new Date());
				devSupplyImeiMapper.insert(dsi);
			} else {
//				dsi.setState(1);
//				dsi.setImei(dsdo.getImei());
//				dsi.setAllotion(1);
				dsi.setId(dsido.getId());
				devSupplyImeiMapper.update(dsi);
			}

		}
		// if (listDevDeviceStaticDO != null && listDevDeviceStaticDO.size() >
		// 0) {
		// devDeviceStaticMapper.insertBatch(listDevDeviceStaticDO);
		// devDeviceOnlineMapper.insertBatch(listDevDeviceOnlineDO);
		// }
		return Result.ok();
	}

	private Map<String, String> validPlant() {
		SysconfDO sys = new SysconfDO();
		sys.setCode("assembly_plant"); // 总装厂
		List<SysconfDO> list = sysconfMapper.selectPage(sys);
		Map<String, String> mapPlant = new HashMap<>();
		// 判断数据库中是否有预制总装厂信息
		if (CollectionUtils.isNotEmpty(list)) {
			for (SysconfDO dapdo : list) {
				mapPlant.put(dapdo.getName(), dapdo.getValue());
			}
		} else {
			Assert.isTrue(false, ImportDataConstants.PLANT_IS_UNMATCH);
		}
		return mapPlant;
	}

	private Map<String, Integer> validCarId() {
		List<DevCarDO> ListDevCarDO = devCarMapper.selectPage(null);
		Map<String, Integer> mapCarType = new HashMap<>();
		// 判断数据库中是否有预制车辆类型信息
		if (ListDevCarDO != null && ListDevCarDO.size() > 0) {
			for (DevCarDO ddo : ListDevCarDO) {
				mapCarType.put(ddo.getTitle(), ddo.getId());
			}
		} else {
			Assert.isTrue(false, ImportDataConstants.CAR_ID_UNMATCH);
		}
		return mapCarType;
	}

	private List<String> validSysConf(String code) {
		List<SysconfDO> ListSysconfDO = sysconfMapper.selectByCode(code);
		List<String> sysCode = new ArrayList<>();
		// 判断数据库中是否有预制信息
		if (ListSysconfDO != null && ListSysconfDO.size() > 0) {
			for (SysconfDO sys : ListSysconfDO) {
				sysCode.add(sys.getName());
			}
		} else {
			if ("color".equals(code)) {
				Assert.isTrue(false, ImportDataConstants.COLOR_UNMATCH);
			} else if ("unlock_way".equals(code)) {
				Assert.isTrue(false, ImportDataConstants.UNLOCK_WAY_UNMATCH);
			}
			Assert.isTrue(false, "预制错误");
		}
		return sysCode;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Result importCertificate(String str) {
		// 转换为目标对象list
		List<DevCertificateDO> list = new ArrayList<>();
		List<DevCertificateDO> listInsert = new ArrayList<>();
		try {
			list = JSONObject.parseArray(str, DevCertificateDO.class);
		} catch (Exception e) {
			String[] split = e.toString().split(":");
			String msg = split[split.length - 1].replace("\"", "");
			Assert.isTrue(false, ImportDataConstants.DATE_ISSUE_FORMAT_ERROR + ":" + msg);
		}
		int i = ImportDataConstants.START_ROW + 1;
		validExcel.validCertificate(list);
		for (DevCertificateDO dcdo : list) {
			i++;
			DevDeviceStaticDO ddsdo = devDeviceStaticMapper.selectOne(ImmutableMap.of("devSn", dcdo.getDevSn()));
			Assert.notNull(ddsdo, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_UNMATCH));
			dcdo.setDevId(ddsdo.getDevId());

			int count = devCertificateMapper.selectCount(ImmutableMap.of("devId", ddsdo.getDevId()));
			Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.CAR_CERT_EXIST));
			count = devCertificateMapper.selectCount(ImmutableMap.of("certificateSn", dcdo.getCertificateSn()));
			Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.CERT_EXIST));
			count = devCertificateMapper.selectCount(ImmutableMap.of("paperNum", dcdo.getPaperNum()));
			Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.PAPER_NUM_FORMAT_EXIST));
			count = devCertificateMapper.selectCount(ImmutableMap.of("labelNum", dcdo.getLabelNum()));
			Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.LABEL_NUM_FORMAT_EXIST));
			count = devCertificateMapper.selectCount(ImmutableMap.of("certificateConformity", dcdo.getCertificateConformity()));
			Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.CONFORMITY_EXIST));
			dcdo.setCdt(new Date());
			listInsert.add(dcdo);
		}
		if (listInsert != null && listInsert.size() > 0) {
			devCertificateMapper.insertBatch(listInsert);
		}
		return Result.ok();
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Result importBatteryPack(String str) {
		List<DevBatteryPackDO> list = JSONObject.parseArray(str, DevBatteryPackDO.class);
		int i = ImportDataConstants.START_ROW;
		LinkedHashSet<String> snSet = Sets.newLinkedHashSet();
		List<DevBatteryPackDO> packs = Lists.newArrayList();
		for (DevBatteryPackDO dbpdo : list) {
			i++;
			Assert.notNull(dbpdo.getDevSn(), ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_IS_NULL));
			Assert.notNull(dbpdo.getPackCode(), ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_IS_NULL));
			DevDeviceStaticDO ddsdo = devDeviceStaticMapper.selectOne(ImmutableMap.of("devSn", dbpdo.getDevSn()));
			Assert.notNull(ddsdo, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_UNMATCH));
			dbpdo.setDevId(ddsdo.getDevId());
			List<DevBatteryPackDO> listDevBatteryPackDO = devBatteryPackMapper.selectByMap(ImmutableMap.of("packCode", dbpdo.getPackCode()));
			Assert.isTrue(listDevBatteryPackDO.size() == 0, ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_EXIST));
			dbpdo.setCdt(new Date());
			packs.add(dbpdo);
			boolean add = snSet.add(dbpdo.getPackCode());
			Assert.isTrue(add, ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_TABLE_REPEAT));
		}
		devBatteryPackMapper.insertBatch(packs);
		return Result.ok();
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Result importBatteryModule(String str) {
		List<DevBatteryModuleDO> list = JSONObject.parseArray(str, DevBatteryModuleDO.class);
		List<DevBatteryModuleDO> modules = Lists.newArrayList();
		HashSet<String> moduleSet = Sets.newLinkedHashSet();
		int i = ImportDataConstants.START_ROW;
		for (DevBatteryModuleDO dbmdo : list) {
			i++;
			Assert.notNull(dbmdo.getPackCode(), ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_IS_NULL));
			Assert.notNull(dbmdo.getModuleCode(), ImportDataConstants.splicing(i, ImportDataConstants.MODULE_CODE_IS_NULL));

			DevBatteryPackDO devBatteryPackDO = devBatteryPackMapper.selectOne(ImmutableMap.of("packCode", dbmdo.getPackCode()));
			Assert.notNull(devBatteryPackDO, ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_UNMATCH));
			dbmdo.setDbpId(devBatteryPackDO.getDbpId());
			List<DevBatteryModuleDO> listDevBatteryModuleDO = devBatteryModuleMapper.selectByMap(ImmutableMap.of("moduleCode", dbmdo.getModuleCode()));
			Assert.isTrue(listDevBatteryModuleDO.size() == 0, ImportDataConstants.splicing(i, ImportDataConstants.MODULE_CODE_EXIST));
			dbmdo.setCdt(new Date());
			boolean add = moduleSet.add(dbmdo.getModuleCode());
			Assert.isTrue(add, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_TABLE_REPEAT));
			modules.add(dbmdo);
		}
		if (modules != null && modules.size() > 0) {
			devBatteryModuleMapper.insertBatch(modules);
		}
		return Result.ok();
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Result importBattery(String str) {
		List<DevBatteryDO> list = JSONObject.parseArray(str, DevBatteryDO.class);
		List<DevBatteryDO> batterys = new LinkedList<>();
		HashSet<String> batterySet = new HashSet<>();
		int i = ImportDataConstants.START_ROW;
		for (DevBatteryDO dbdo : list) {
			i++;
			Assert.notNull(dbdo.getBatteryCode(), ImportDataConstants.splicing(i, ImportDataConstants.BATTERY_CODE_IS_NULL));
			Assert.notNull(dbdo.getModuleCode(), ImportDataConstants.splicing(i, ImportDataConstants.MODULE_CODE_IS_NULL));

			DevBatteryModuleDO DevBatteryModuleDO = devBatteryModuleMapper.selectOne(ImmutableMap.of("moduleCode", dbdo.getModuleCode()));
			Assert.notNull(DevBatteryModuleDO, ImportDataConstants.splicing(i, ImportDataConstants.MODULE_CODE_UNMATCH));
			dbdo.setDbmId(DevBatteryModuleDO.getDbmId());

			List<DevBatteryDO> listDevBatteryDO = devBatteryMapper.selectPage(ImmutableMap.of("batteryCode", dbdo.getBatteryCode()));
			Assert.isTrue(listDevBatteryDO.size() == 0, ImportDataConstants.splicing(i, ImportDataConstants.BATTERY_CODE_EXIST));
			dbdo.setCdt(new Date());
			boolean add = batterySet.add(dbdo.getBatteryCode());
			Assert.isTrue(add, ImportDataConstants.splicing(i, ImportDataConstants.BATTERY_CODE_TABLE_REPEAT));
			batterys.add(dbdo);
		}
		if (batterys != null && batterys.size() > 0) {
			devBatteryMapper.insertBatch(batterys);
		}
		return Result.ok();
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Result importDeviceParts(String str) {
		// 转换为目标对象list
		List<DevDevicePartsDO> list = JSONObject.parseArray(str, DevDevicePartsDO.class);
		List<DevDevicePartsDO> listDevDevicePartsDO = new ArrayList<>();
		HashSet<Map<Integer, String>> setMap = new HashSet<>();

		int i = ImportDataConstants.START_ROW + 1;
		for (DevDevicePartsDO ddpdo : list) {
			Map<Integer, String> map = new HashMap<>();
			i++;
			validParts(i, ddpdo);
			DevDeviceStaticDO ddsdo = devDeviceStaticMapper.selectOne(ImmutableMap.of("devSn", ddpdo.getDevSn()));
			Assert.notNull(ddsdo, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_UNMATCH));
			int count = devBomPartpnRefMapper.selectCount(ImmutableMap.of("bomId", ddsdo.getBomId(), "partsPn", ddpdo.getPartsPn()));
			Assert.isTrue(count != 0, ImportDataConstants.splicing(i, ImportDataConstants.PARTS_PN_UNMATCH));

			int countDevId = devDevicePartsMapper.selectCount(ImmutableMap.of("devId", ddsdo.getDevId()));
			Assert.isTrue(countDevId == 0, ImportDataConstants.splicing(i, ImportDataConstants.CAR_PARTS_EXIST));
			ddpdo.setDevId(ddsdo.getDevId());
			ddpdo.setCdt(new Date());
			map.put(ddpdo.getDevId(), ddpdo.getPartsPn());
			setMap.add(map);
			Assert.isTrue(setMap.size() == (i - 2), ImportDataConstants.splicing(i, ImportDataConstants.PARTS_PN_TABLE_EXIST));
			listDevDevicePartsDO.add(ddpdo);
		}
		for (DevDevicePartsDO ddpdo : listDevDevicePartsDO) {
			devDevicePartsMapper.insert(ddpdo);
		}
		return Result.ok();
	}

	private void validParts(int i, DevDevicePartsDO ddpdo) {
		Assert.notNull(ddpdo.getDevSn(), ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_IS_NULL));
		Assert.notNull(ddpdo.getPartsPn(), ImportDataConstants.splicing(i, ImportDataConstants.PARTS_PN_IS_NULL));
		Assert.isTrue(ddpdo.getPartsSn() == null || ddpdo.getPartsSn().length() <= 50, ImportDataConstants.splicing(i, ImportDataConstants.PARTS_SN_FORMAT_ERROR));
		Assert.isTrue(ddpdo.getBatchNum() == null || ddpdo.getBatchNum().length() <= 50, ImportDataConstants.splicing(i, ImportDataConstants.BATCH_NUM_FORMAT_ERROR));
		Assert.isTrue(ddpdo.getHardVersion() == null || ddpdo.getHardVersion().length() <= 50, ImportDataConstants.splicing(i, ImportDataConstants.HARD_VERSION_FORMAT_ERROR));
		Assert.isTrue(ddpdo.getSoftVersion() == null || ddpdo.getSoftVersion().length() <= 50, ImportDataConstants.splicing(i, ImportDataConstants.SOFT_VERSION_FORMAT_ERROR));
	}

	@Override
	public Result updateDevice(String str) {

		List<String> color = validSysConf("color");
		List<String> unlockWay = validSysConf("unlock_way");
		List<String> plant = validSysConf("assembly_plant");
		Map<String, Integer> mapCarType = validCarId();
		Map<String, String> mapPlant = validPlant();
		// 转换为目标对象list
		List<DevDeviceStaticDO> list = new ArrayList<>();
		try {
			list = JSONObject.parseArray(str, DevDeviceStaticDO.class);
		} catch (Exception e) {
			String[] split = e.toString().split(":");
			String msg = split[split.length - 1].replace("\"", "");
			Assert.isTrue(false, ImportDataConstants.BUILDTIME_FORMAT_ERROR + ":" + msg);
		}
		validExcel.validDevice(list, color, unlockWay, mapCarType, plant, 2);

		int i = ImportDataConstants.START_ROW + 1;
		DevBomCarDO DevBomCarDO = null;
		DevDeviceStaticDO DevDeviceStaticDO = null;
		for (DevDeviceStaticDO dsdo : list) {
			i++;
			dsdo.setCarId(mapCarType.get(dsdo.getCarType()));
			dsdo.setDapCity(mapPlant.get(dsdo.getDapName()));
			DevBomCarDO = devBomCarMapper.selectOne(ImmutableMap.of("carId", dsdo.getCarId(), "bomVersion", dsdo.getBomVersion()));
			Assert.isTrue(DevBomCarDO != null, ImportDataConstants.splicing(i, ImportDataConstants.BOM_VERSION_UNMATCH));
			dsdo.setBomId(DevBomCarDO.getBomId());
			DevDeviceStaticDO DevDeviceStaticDO1 = devDeviceStaticMapper.selectOne(ImmutableMap.of("devSn", dsdo.getDevSn()));
			Assert.isTrue(DevDeviceStaticDO1 != null, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_UNMATCH));
			dsdo.setDevId(DevDeviceStaticDO1.getDevId());
			
			
			DevDeviceStaticDO = devDeviceStaticMapper.selectOne(ImmutableMap.of("imei", dsdo.getImei()));
			if(DevDeviceStaticDO!=null){
				Assert.isTrue(DevDeviceStaticDO.getDevSn().equals(dsdo.getDevSn()), ImportDataConstants.splicing(i, ImportDataConstants.IMEI_EXIST));
			}
			DevDeviceStaticDO = devDeviceStaticMapper.selectOne(ImmutableMap.of("tbox", dsdo.getTbox()));
			if(DevDeviceStaticDO!=null){
				Assert.isTrue(DevDeviceStaticDO.getDevSn().equals(dsdo.getDevSn()), ImportDataConstants.splicing(i, ImportDataConstants.TBOX_EXIST));
			}
			DevDeviceStaticDO = devDeviceStaticMapper.selectOne(ImmutableMap.of("bmsNo", dsdo.getBmsNo()));
			if(DevDeviceStaticDO!=null){
				Assert.isTrue(DevDeviceStaticDO.getDevSn().equals(dsdo.getDevSn()), ImportDataConstants.splicing(i, ImportDataConstants.BMSNO_EXIST));
			}
			
			if (StringUtils.isNotBlank(dsdo.getVin())) {
				DevDeviceStaticDO dsd = devDeviceStaticMapper.selectOne(ImmutableMap.of("vin", dsdo.getVin()));
				if (dsd != null) {
					Assert.isTrue(dsdo.getDevSn().equals(dsd.getDevSn()), ImportDataConstants.splicing(i, ImportDataConstants.CARNUB_EXIST));
				}
			}
			dsdo.setStatus(StaticEnum.NO_DISTRIBUTION.getState());
			dsdo.setUdt(new Date());
			devDeviceStaticMapper.update(dsdo);

			List<DevDevicePartsDO> listDevDevicePartsDO = devDevicePartsMapper.selectPage(ImmutableMap.of("devId", DevDeviceStaticDO1.getDevId()));
			if (listDevDevicePartsDO != null && listDevDevicePartsDO.size() > 0) {
				for (DevDevicePartsDO ddpdo : listDevDevicePartsDO) {
					List<DevBomPartpnRefDO> listDevBomPartpnRefDO = devBomPartpnRefMapper.selectPage(ImmutableMap.of("partsPn", ddpdo.getPartsPn(), "bomId", DevDeviceStaticDO1.getBomId()));
					if (listDevBomPartpnRefDO != null && listDevBomPartpnRefDO.size() > 0) {
						DevBomPartpnRefDO DevBomPartpnRefDO = listDevBomPartpnRefDO.get(0);
						if (DevBomPartpnRefDO != null && DevBomPartpnRefDO.getId() > 0) {

						} else {
							devDevicePartsMapper.deleteById(ddpdo.getRefId());
						}
					}
				}
			}
		}
		return Result.ok();
	}

	@Override
	public Result updateCertificate(String str) {
		// 转换为目标对象list
		List<DevCertificateDO> list = new ArrayList<>();
		try {
			list = JSONObject.parseArray(str, DevCertificateDO.class);
		} catch (Exception e) {
			String[] split = e.toString().split(":");
			String msg = split[split.length - 1].replace("\"", "");
			Assert.isTrue(false, ImportDataConstants.DATE_ISSUE_FORMAT_ERROR + ":" + msg);
		}
		int i = ImportDataConstants.START_ROW + 1;
		validExcel.validCertificate(list);
		DevDeviceStaticDO ddsdo = new DevDeviceStaticDO();
		for (DevCertificateDO dcdo : list) {
			i++;
			// 查询合格证中的车辆序列号在基本信息中是否存在
			ddsdo = devDeviceStaticMapper.selectOne(ImmutableMap.of("devSn", dcdo.getDevSn()));
			Assert.notNull(ddsdo, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_UNMATCH));
			dcdo.setDevId(ddsdo.getDevId());
			dcdo.setUdt(new Date());

			// 查询序列号在合格证中是否存在
			DevCertificateDO dc = devCertificateMapper.selectOne(ImmutableMap.of("devId", dcdo.getDevId()));
			if (dc == null) { // 不存在，新增
				int count = devCertificateMapper.selectDevIdOrCertSnOrLabelNumOrPaperNumOrCertConfNoCertId(ImmutableMap.of("certificateSn", dcdo.getCertificateSn()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.CERT_EXIST));
				count = devCertificateMapper.selectDevIdOrCertSnOrLabelNumOrPaperNumOrCertConfNoCertId(ImmutableMap.of("labelNum", dcdo.getLabelNum()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.LABEL_NUM_FORMAT_EXIST));
				count = devCertificateMapper.selectDevIdOrCertSnOrLabelNumOrPaperNumOrCertConfNoCertId(ImmutableMap.of("paperNum", dcdo.getPaperNum()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.PAPER_NUM_FORMAT_EXIST));
				count = devCertificateMapper.selectDevIdOrCertSnOrLabelNumOrPaperNumOrCertConfNoCertId(ImmutableMap.of("certificateConformity", dcdo.getCertificateConformity()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.CONFORMITY_EXIST));
				devCertificateMapper.insert(dcdo);
			} else {// 存在 更新
				dcdo.setCertId(dc.getCertId());
				int count = devCertificateMapper.selectDevIdOrCertSnOrLabelNumOrPaperNumOrCertConfNoCertId(ImmutableMap.of("certId", dc.getCertId(), "certificateSn", dcdo.getCertificateSn()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.CERT_EXIST));
				count = devCertificateMapper.selectDevIdOrCertSnOrLabelNumOrPaperNumOrCertConfNoCertId(ImmutableMap.of("certId", dc.getCertId(), "labelNum", dcdo.getLabelNum()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.LABEL_NUM_FORMAT_EXIST));
				count = devCertificateMapper.selectDevIdOrCertSnOrLabelNumOrPaperNumOrCertConfNoCertId(ImmutableMap.of("certId", dc.getCertId(), "paperNum", dcdo.getPaperNum()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.PAPER_NUM_FORMAT_EXIST));
				count = devCertificateMapper
						.selectDevIdOrCertSnOrLabelNumOrPaperNumOrCertConfNoCertId(ImmutableMap.of("certId", dc.getCertId(), "certificateConformity", dcdo.getCertificateConformity()));
				Assert.isTrue(count == 0, ImportDataConstants.splicing(i, ImportDataConstants.CONFORMITY_EXIST));
				devCertificateMapper.update(dcdo);
			}

		}
		return Result.ok();
	}

	@Override
	public Result updateDeviceParts(String str) {
		// 转换为目标对象list
		List<DevDevicePartsDO> list = JSONObject.parseArray(str, DevDevicePartsDO.class);
		List<DevDevicePartsDO> listDevDevicePartsDOInsert = new ArrayList<>();
		List<DevDevicePartsDO> listDevDevicePartsDOUpdate = new ArrayList<>();
		HashSet<Map<Integer, String>> setMap = new HashSet<>();

		int i = ImportDataConstants.START_ROW + 1;
		for (DevDevicePartsDO ddpdo : list) {
			Map<Integer, String> map = new HashMap<>();
			i++;
			Assert.notNull(ddpdo.getDevSn(), ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_IS_NULL));
			Assert.notNull(ddpdo.getPartsPn(), ImportDataConstants.splicing(i, ImportDataConstants.PARTS_PN_IS_NULL));

			if (StringUtils.isNotBlank(ddpdo.getPartsSn())) {
				Assert.isTrue(ddpdo.getPartsSn().length() <= 50, ImportDataConstants.splicing(i, ImportDataConstants.PARTS_SN_FORMAT_ERROR));
			}
			if (StringUtils.isNotBlank(ddpdo.getBatchNum())) {
				Assert.isTrue(ddpdo.getBatchNum().length() <= 50, ImportDataConstants.splicing(i, ImportDataConstants.BATCH_NUM_FORMAT_ERROR));
			}
			if (StringUtils.isNotBlank(ddpdo.getHardVersion())) {
				Assert.isTrue(ddpdo.getHardVersion().length() <= 50, ImportDataConstants.splicing(i, ImportDataConstants.HARD_VERSION_FORMAT_ERROR));
			}
			if (StringUtils.isNotBlank(ddpdo.getSoftVersion())) {
				Assert.isTrue(ddpdo.getSoftVersion().length() <= 50, ImportDataConstants.splicing(i, ImportDataConstants.SOFT_VERSION_FORMAT_ERROR));
			}

			DevDeviceStaticDO ddsdo = devDeviceStaticMapper.selectOne(ImmutableMap.of("devSn", ddpdo.getDevSn()));
			Assert.notNull(ddsdo, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_UNMATCH));
			int count = devBomPartpnRefMapper.selectCount(ImmutableMap.of("bomId", ddsdo.getBomId(), "partsPn", ddpdo.getPartsPn()));
			Assert.isTrue(count != 0, ImportDataConstants.splicing(i, ImportDataConstants.PARTS_PN_UNMATCH));
			ddpdo.setDevId(ddsdo.getDevId());
			ddpdo.setUdt(new Date());
			map.put(ddpdo.getDevId(), ddpdo.getPartsPn());
			boolean add = setMap.add(map);
			Assert.isTrue(add, ImportDataConstants.splicing(i, ImportDataConstants.PARTS_PN_TABLE_EXIST));

			DevDevicePartsDO DevDeviceParts = devDevicePartsMapper.selectOne(ImmutableMap.of("devId", ddsdo.getDevId(), "partsPn", ddpdo.getPartsPn()));
			if (DevDeviceParts == null) {
				listDevDevicePartsDOInsert.add(ddpdo);
			} else {
				ddpdo.setRefId(DevDeviceParts.getRefId());
				listDevDevicePartsDOUpdate.add(ddpdo);
			}
		}
		// if(listDevDevicePartsDOInsert!=null&&listDevDevicePartsDOInsert.size()>0){
		// devDevicePartsMapper.in
		// }
		for (DevDevicePartsDO ddpdo : listDevDevicePartsDOInsert) {
			devDevicePartsMapper.insert(ddpdo);
		}
		for (DevDevicePartsDO ddpdo : listDevDevicePartsDOUpdate) {
			devDevicePartsMapper.updateDevDeviceParts(ddpdo);
		}
		return Result.ok();
	}

	@Override
	public Result updateBatteryPack(String str) {
		List<DevBatteryPackDO> list = JSONObject.parseArray(str, DevBatteryPackDO.class);
		int i = ImportDataConstants.START_ROW;
		LinkedHashSet<String> snSet = Sets.newLinkedHashSet();
		List<DevBatteryPackDO> packs = Lists.newArrayList();
		for (DevBatteryPackDO dbpdo : list) {
			i++;
			Assert.notNull(dbpdo.getDevSn(), ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_IS_NULL));
			Assert.notNull(dbpdo.getPackCode(), ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_IS_NULL));
			DevDeviceStaticDO ddsdo = devDeviceStaticMapper.selectOne(ImmutableMap.of("devSn", dbpdo.getDevSn()));
			Assert.notNull(ddsdo, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_UNMATCH));
			dbpdo.setDevId(ddsdo.getDevId());
			dbpdo.setUdt(new Date());
			boolean add = snSet.add(dbpdo.getPackCode());
			Assert.isTrue(add, ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_TABLE_REPEAT));

			DevBatteryPackDO DevBatteryPackDO = devBatteryPackMapper.selectOne(ImmutableMap.of("packCode", dbpdo.getPackCode(), "devId", dbpdo.getDevId()));
			if (DevBatteryPackDO == null) {
				DevBatteryPackDO DevBatteryPack = devBatteryPackMapper.selectPackCodeNotDevSn(ImmutableMap.of("packCode", dbpdo.getPackCode(), "devId", dbpdo.getDevId()));
				Assert.isNull(DevBatteryPack, ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_EXIST));
				packs.add(dbpdo);
			}
		}
		if (packs != null && packs.size() > 0) {
			devBatteryPackMapper.insertBatch(packs);
		}
		return Result.ok();
	}

	@Override
	public Result updateBatteryModule(String str) {
		List<DevBatteryModuleDO> list = JSONObject.parseArray(str, DevBatteryModuleDO.class);
		List<DevBatteryModuleDO> modules = Lists.newArrayList();
		HashSet<String> moduleSet = Sets.newLinkedHashSet();
		int i = ImportDataConstants.START_ROW;
		for (DevBatteryModuleDO dbmdo : list) {
			i++;
			Assert.notNull(dbmdo.getPackCode(), ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_IS_NULL));
			Assert.notNull(dbmdo.getModuleCode(), ImportDataConstants.splicing(i, ImportDataConstants.MODULE_CODE_IS_NULL));
			DevBatteryPackDO devBatteryPackDO = devBatteryPackMapper.selectOne(ImmutableMap.of("packCode", dbmdo.getPackCode()));
			Assert.notNull(devBatteryPackDO, ImportDataConstants.splicing(i, ImportDataConstants.PACK_CODE_UNMATCH));
			dbmdo.setDbpId(devBatteryPackDO.getDbpId());
			dbmdo.setUdt(new Date());
			boolean add = moduleSet.add(dbmdo.getModuleCode());
			Assert.isTrue(add, ImportDataConstants.splicing(i, ImportDataConstants.DEV_SN_TABLE_REPEAT));

			DevBatteryModuleDO devBatteryModuleDO = devBatteryModuleMapper.selectOne(ImmutableMap.of("moduleCode", dbmdo.getModuleCode(), "dbpId", dbmdo.getDbpId()));
			if (devBatteryModuleDO == null) {
				DevBatteryModuleDO devBatteryModule = devBatteryModuleMapper.selectModuleNotdbpId(ImmutableMap.of("moduleCode", dbmdo.getModuleCode(), "dbpId", dbmdo.getDbpId()));
				Assert.isNull(devBatteryModule, ImportDataConstants.splicing(i, ImportDataConstants.MODULE_CODE_EXIST));
				modules.add(dbmdo);
			}
		}
		if (modules != null && modules.size() > 0) {

			devBatteryModuleMapper.insertBatch(modules);
		}
		return Result.ok();
	}

	@Override
	public Result updateBattery(String str) {
		List<DevBatteryDO> list = JSONObject.parseArray(str, DevBatteryDO.class);
		List<DevBatteryDO> insertBatterys = new LinkedList<>();
		HashSet<String> batterySet = new HashSet<>();
		int i = ImportDataConstants.START_ROW;
		for (DevBatteryDO dbdo : list) {
			i++;
			Assert.notNull(dbdo.getBatteryCode(), ImportDataConstants.splicing(i, ImportDataConstants.BATTERY_CODE_IS_NULL));
			Assert.notNull(dbdo.getModuleCode(), ImportDataConstants.splicing(i, ImportDataConstants.MODULE_CODE_IS_NULL));

			DevBatteryModuleDO DevBatteryModuleDO = devBatteryModuleMapper.selectOne(ImmutableMap.of("moduleCode", dbdo.getModuleCode()));
			Assert.notNull(DevBatteryModuleDO, ImportDataConstants.splicing(i, ImportDataConstants.MODULE_CODE_UNMATCH));
			dbdo.setDbmId(DevBatteryModuleDO.getDbmId());
			dbdo.setUdt(new Date());
			boolean add = batterySet.add(dbdo.getBatteryCode());
			Assert.isTrue(add, ImportDataConstants.splicing(i, ImportDataConstants.BATTERY_CODE_TABLE_REPEAT));
			DevBatteryDO DevBatteryDO = devBatteryMapper.selectOne(ImmutableMap.of("batteryCode", dbdo.getBatteryCode(), "dbmId", dbdo.getDbmId()));
			if (DevBatteryDO == null) {
				DevBatteryDO DevBattery = devBatteryMapper.selectBatteryNotDbmId(ImmutableMap.of("batteryCode", dbdo.getBatteryCode(), "dbmId", dbdo.getDbmId()));
				Assert.isNull(DevBattery, ImportDataConstants.splicing(i, ImportDataConstants.BATTERY_CODE_EXIST));
				insertBatterys.add(dbdo);
			}
		}
		if (insertBatterys != null && insertBatterys.size() > 0) {
			devBatteryMapper.insertBatch(insertBatterys);
		}
		return Result.ok();
	}

}
